package com.prevpn.api;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.prevpn.common.CommonResponse;
import com.prevpn.common.CommonUtil;
import com.prevpn.vo.UserDetailVO;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * UserApi - 调用 aTrust OpenAPI 用户接口
 */
@Component
public class UserApi {

    /**
     * 新增用户接口
     * 对应：POST /api/v3/user/create
     */
    private static final String CREATE_USER_PATH = "/api/v3/user/create";

    /**
     * 新增用户接口（Map 参数形式）
     * 对应：POST /api/v3/user/create
     */
    public CommonResponse<Map<String, Object>> createUser(Map<String, Object> body) {
        String jsonBody = JSONUtil.toJsonStr(body);
        String response = CommonUtil.openApiPostRequest(CREATE_USER_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 调用 aTrust 查询用户详情接口 /api/v3/user/queryById
     */
    public CommonResponse<UserDetailVO> queryUserById(String directoryDomain, String id) {
        String path = "/api/v3/user/queryById";
        Map<String, Object> params = new HashMap<>();
        params.put("directoryDomain", directoryDomain);
        params.put("id", id);

        String response = CommonUtil.openApiGetRequest(path, params);

        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<UserDetailVO>>() {},
                false);
    }

    /**
     * 查询用户列表（精确匹配邮箱）
     */
    public CommonResponse<Map<String, Object>> queryUserListByEmail(String directoryDomain, String email) {
        String path = "/api/v3/user/queryAll";

        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("pageSize", 100);
        body.put("pageIndex", 1);
        body.put("fuzzyMatch", email);  // 邮箱是唯一的，不需要模糊匹配

        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));

        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 查询用户列表（分页 + 递归 + 模糊搜索）
     */
    public CommonResponse<Map<String, Object>> queryAllUsers(Map<String, Object> body) {
        String path = "/api/v3/user/queryAll";
        String requestBody = JSONUtil.toJsonStr(body);

        // 调用 aTrust OpenAPI
        String response = CommonUtil.openApiPostRequest(path, requestBody);

        // 解析响应
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 根据用户名查询用户详情
     */
    public CommonResponse<Map<String, Object>> queryUserByName(String directoryDomain, String name) {
        String path = "/api/v3/user/queryByName";

        // 构造 GET 参数
        Map<String, Object> params = new HashMap<>();
        params.put("directoryDomain", directoryDomain);
        params.put("name", name);

        // 调用 OpenAPI
        String response = CommonUtil.openApiGetRequest(path, params);

        // 解析为通用响应对象
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false);
    }

    /**
     * 关联角色（基于用户名）
     */
    public CommonResponse<Map<String, Object>> assignRoleByName(String directoryDomain,
                                                                String name,
                                                                Map<String, Object> role) {
        String path = "/api/v3/user/assignRoleByName";

        // 组装请求体
        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("name", name);
        body.put("role", role);

        // 发送 POST 请求
        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));

        // 转换响应
        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {},
                false);
    }


    private static final String BULK_DELETE_USER_PATH = "/api/v3/user/bulkDeleteByNameList";

    /**
     * 批量删除用户 - 基于名称
     */
    public CommonResponse<Object> bulkDeleteUserByNameList(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(BULK_DELETE_USER_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Object>>() {}, false);
    }

    private static final String UPDATE_USER_PATH = "/api/v3/user/updateByName";

    /**
     * 调用 OpenAPI 编辑用户 - 基于名称
     */
    public CommonResponse<Object> updateUserByName(String jsonBody) {
        String response = CommonUtil.openApiPostRequest(UPDATE_USER_PATH, jsonBody);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Object>>() {}, false);
    }

}
